package Beans;

import util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PCBDAO {
    private static final String FindAllPCB = "select * from PCBlist where status = 1";
    private static final String InsertPCB = "insert into PCBlist (Pid,timeRequired,Priority,status) values (?,?,?,?)";
//    private static final String DeletePCB = "delete from PCBlist where Pid = ?";
    private static final String UpdatePCB = "update PCBlist set Pid=?,timeRequired=?,Priority=?,status=? where Pid=?";

    public static List<PCB> findAllPCB() {
        List<PCB> PCBlist = new ArrayList<>();
        try{
            Connection connection = DBUtil.getConnection();
            Statement statement=connection.createStatement();
            ResultSet resultSet=statement.executeQuery(FindAllPCB);
            while(resultSet.next()){
                PCB pcb=new PCB();
                pcb.setPid(resultSet.getString("Pid"));
                pcb.setTimeRequired(resultSet.getInt("timeRequired"));
                pcb.setPriority(resultSet.getInt("Priority"));
                pcb.setId(resultSet.getInt("id"));
                pcb.setStatus(resultSet.getInt("status"));
                pcb.setStorageRequierd(resultSet.getInt("memoryRequired"));

                PCBlist.add(pcb);
            }
            DBUtil.close(connection,statement,resultSet);
        }catch (Exception e){
            e.printStackTrace();
        }
        return PCBlist;
    }

    public static int insertPCB(PCB pcb){
        int result = 0;
        try {
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement =connection.prepareStatement(InsertPCB);
            if(pcb!=null){
                preparedStatement.setString(1, pcb.getPid());
                preparedStatement.setInt(2,pcb.getTimeRequired());
                preparedStatement.setInt(3,pcb.getPriority());
                preparedStatement.setInt(4,pcb.getStatusValue());
            }
            result = preparedStatement.executeUpdate();
            DBUtil.close(connection,preparedStatement,null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int updatePCB(PCB pcb){
        int result=0;
        try {
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(UpdatePCB);
            if(pcb!=null){
                preparedStatement.setString(1, pcb.getPid());
                preparedStatement.setInt(2,pcb.getTimeRequired());
                preparedStatement.setInt(3,pcb.getPriority());
                preparedStatement.setInt(4,pcb.getStatusValue());
                preparedStatement.setString(5,pcb.getPid());
            }
            result=preparedStatement.executeUpdate();
            DBUtil.close(connection,preparedStatement,null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}
